package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user/order")
public class ApiOrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 提交订单
     *
     * @param ordersSubmitDTO 订单提交参数
     * @return 订单提交结果
     */
    @PostMapping("/submit")
    @ApiOperation(value = "提交订单")
    public Result<OrderSubmitVO> submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("下单参数: {}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = ordersService.submit(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO 订单支付参数
     * @return 支付结果
     */
    @PutMapping("/payment")
    @ApiOperation(value = "订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付: {}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单: {}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 历史订单
     *
     * @return 历史订单列表
     */
    @GetMapping("/historyOrders")
    @ApiOperation(value = "历史订单")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("历史订单查询: {}", ordersPageQueryDTO);
        PageResult pageResult = ordersService.historyOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 订单详情
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    @GetMapping("/orderDetail/{orderId}")
    @ApiOperation(value = "订单详情")
    public Result<OrderVO> getOrderDetail(@PathVariable("orderId") Long orderId) {
        log.info("订单详情: {}", orderId);
        OrderVO orderVO = ordersService.getOrderDetail(orderId);
        return Result.success(orderVO);
    }

    /**
     * 取消订单
     *
     * @param orderId 订单ID
     * @return 取消结果
     */
    @PutMapping("cancel/{orderId}")
    @ApiOperation(value = "取消订单")
    public Result<String> cancel(@PathVariable("orderId") Long orderId) throws Exception {
        log.info("取消订单: {}", orderId);
        ordersService.cancelByUser(orderId);
        return Result.success();
    }

    /**
     * 再来一单
     *
     * @param orderId 订单ID
     * @return 再来一单结果
     */
    @PostMapping("/repetition/{orderId}")
    @ApiOperation(value = "再来一单")
    public Result<String> repetition(@PathVariable("orderId") Long orderId) {
        log.info("再来一单: {}", orderId);
        ordersService.repetition(orderId);
        return Result.success();
    }

    /**
     * 用户催单
     *
     * @param orderId 订单ID
     * @return 催单结果
     */
    @GetMapping("/reminder/{orderId}")
    @ApiOperation(value = "催单")
    public Result<String> reminder(@PathVariable("orderId") Long orderId) {
        log.info("催单: {}", orderId);
        ordersService.reminder(orderId);
        return Result.success();
    }

}
